package bTree.LeetCode;

import org.w3c.dom.Node;

import java.util.HashMap;
import java.util.Map;

public class 随机链表的复制 {
    public Node copyRandomList(Node head) {
        Map<Node, Node>map=new HashMap<>();
        Node p1=head;
        while (null!=p1){
            map.put(p1,new Node(p1.val));
            p1=p1.next;
        }
        Node p2=head;
        Node newHead=map.get(p2);
        Node pre=newHead;
        while (null!=p2){
            pre.next=map.get(p2.next);
            pre.random=map.get(p2.random);
            p2=p2.next;
            pre=pre.next;
        }
        return newHead;
    }
}